const {Tool, API, Event,Storage} = global.$util

Page({
    data: {
        region:{},
        receiver: '',
        recevicePhone: '',
        address: '',
        addressList: {},
        id: '',
        hidden: false,
        isDefault: false,
        visible:false
    },
    onLoad: function (options) {
        // 如果有参数 就编辑地址 无参数就创建地址
        let list = options.address
        if (list !== undefined) {
            list = JSON.parse(options.address)
            let {
                province='',
                city='',
                area='',
                street='',
                receiver='',
                receiverPhone='',
                address='',
                id='',
                isDefault='',
                defaultStatus='',
            } = list
            list.des = `${province||''} - ${city||''} - ${area||''} ${street? ' - '+street:""}`
            this.data.isDefault = (defaultStatus===1 || isDefault===1)? true:false
            this.setData({
                receiver,
                receiverPhone,
                address,
                region: list,
                id
            })
            wx.setNavigationBarTitle({
                title: '编辑收货地址'
            })
        }
        this.data.door = options.door || ''
        this.loading = false
    },
    updateNewAddress(e){
        let region = e.detail
        let {
            province,
            city,
            area,
            street
        } = region
        region.des = `${province||''} - ${city||''} - ${area||''} ${street? ' - '+street:""}`
        this.setData({
            region
        })
    },
    changeInput(e){
        let name = e.currentTarget.dataset.name
        this.data[name] = e.detail.value
    },
    formSubmit(e) {
        let {
            receiver,
            receiverPhone,
            address,
            region
        } = this.data

        if (!(receiver.length > 1 && receiver.length < 17)) {
            Tool.showToast("收货人姓名长度需在2-16位之间");
            return
        }

        if (!Tool.checkPhone(receiverPhone|| '')) {
            Tool.showToast("请输入正确的电话号码");
            return
        }

        if (!region.province) {
            Tool.showToast("请选择你所在的省市区");
            return
        }

        if (Tool.isEmptyStr(address)) {
            Tool.showToast("请输入详细地址");
            return
        }
        if(this.loading){
            return;
        }
        this.loading = true
        let params = {
            receiver,
            receiverPhone,
            address,
            provinceCode:region.provinceCode || '',
            cityCode:region.cityCode || '',
            areaCode:region.areaCode || '',
            streetCode:region.streetCode || '',
        }

        this.saveUserAddress(params)

    },
    saveUserAddress(params) { // 添加地址/修改地址
        params = {
            ...params,
            defaultStatus: this.data.isDefault ? 1 : 2,
            id: this.data.id ? this.data.id : ''
        }
        API.addUserAddress(params).then((res) => {
            let tips = this.data.id? '修改成功':'添加成功'
            let data = res.data || {}
            if(this.data.door==2){
                let {
                    province='',
                    city='',
                    area='',
                    street=''
                } = this.data.region
                Storage.setOrderAddress({
                    ...params,
                    id:data.id,
                    province,
                    city,
                    area,
                    street
                })
                Event.emit('updateOrderAddress')
            } else {
                Event.emit('updateAddressList');//发出通知
            }
            Tool.showSuccessToast(tips, ()=>{
                Tool.navigationPop()
                this.loading = false
            })
        }).catch((res) => {
            this.loading = false
            console.log(res)
        })
    },
    setDefault(){
        this.setData({
            isDefault: !this.data.isDefault
        })
    },
    areaClicked(){
        this.setData({
            visible:!this.data.visible
        })
    },
    onUnload(){

    }
})